package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.config.Options.getAction
import com.benjaminwan.read.free.model.ItemConfig
import com.benjaminwan.read.free.model.action.MouseMove
import com.benjaminwan.read.free.model.action.Position
import com.benjaminwan.read.free.model.enums.ItemType
import com.benjaminwan.read.free.model.listOfItemConfig
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.utils.getRegexFilter
import com.benjaminwan.read.free.utils.numRegex
import com.benjaminwan.read.free.utils.showRightTextInputDialog
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

object MouseMoveConfig {
    fun getMouseMoveConfig(mainVM: MainViewModel, pos: Position): List<ItemConfig> {
        return listOfItemConfig {
            itemConfig {
                type = ItemType.Text
                titleStr = "x轴移动距离"
                rightTextFunc = { getAction<MouseMove>(mainVM, pos)?.dx?.toString() ?: "" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 1, 4, getRegexFilter(numRegex, "[-]"),
                        { dialog, which ->
                            val default = "0"
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseMove>(mainVM, pos) ?: return@showRightTextInputDialog
                        val delta = input.toInt()
                        mainVM.setAction(pos, action.copy(dx = delta))
                        dialog.dismiss()
                    }
                }
            }

            itemConfig {
                type = ItemType.Text
                titleStr = "y轴移动距离"
                rightTextFunc = { getAction<MouseMove>(mainVM, pos)?.dy?.toString() ?: "" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 1, 4, getRegexFilter(numRegex, "[-]"),
                        { dialog, which ->
                            val default = "0"
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseMove>(mainVM, pos) ?: return@showRightTextInputDialog
                        val delta = input.toInt()
                        mainVM.setAction(pos, action.copy(dy = delta))
                        dialog.dismiss()
                    }
                }
            }
        }
    }
}